package service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import common.Split;
import dbconnection.ConnectionFactory;
import service.Event;


public class EventImpl implements Event{

	@Override
	public String eventBack(String str) throws Exception {
        int index= (int)(1+Math.random()*10);
        System.out.println(index+"-------------");
        ArrayList<String> list=new ArrayList<>();
        ArrayList<String> backList=new ArrayList<>();
        Split split=new Split();
        list=split.splitList(str);
        ConnectionFactory connectionFactory=new ConnectionFactory();
        Connection connection=connectionFactory.getConnection();
        String sql="select text,binderRatio,confidence,luck,health,money from jc_event where id=?";
        PreparedStatement preparedStatement;
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setInt(1,index);
        ResultSet resultSet=preparedStatement.executeQuery();
        while(resultSet.next()){
            backList.add(resultSet.getString("text"));
            backList.add(Integer.parseInt(list.get(1))+Integer.parseInt(resultSet.getString("binderRatio"))+"");
            backList.add(Integer.parseInt(list.get(2))+Integer.parseInt(resultSet.getString("confidence"))+"");
            backList.add(Integer.parseInt(list.get(3))+Integer.parseInt(resultSet.getString("luck"))+"");
            backList.add(Integer.parseInt(list.get(4))+Integer.parseInt(resultSet.getString("health"))+"");
            backList.add(Integer.parseInt(list.get(5))+Integer.parseInt(resultSet.getString("money"))+"");
        }
        int ok=0;
        sql="update jc_userproperty set binderRatio=?,confidence=?,luck=?,health=?,money=? where userId=?";
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
        preparedStatement.setInt(1, Integer.parseInt(backList.get(1)));
        preparedStatement.setInt(2, Integer.parseInt(backList.get(2)));
        preparedStatement.setInt(3, Integer.parseInt(backList.get(3)));
        preparedStatement.setInt(4, Integer.parseInt(backList.get(4)));
        preparedStatement.setInt(5, Integer.parseInt(backList.get(5)));
        preparedStatement.setInt(6, Integer.parseInt(list.get(0)));
        ok=preparedStatement.executeUpdate();
        String eventText="";
        for(int i=0;i<backList.size();i++){
            eventText+=backList.get(i);
            if(i!=backList.size()-1&&i!=0){
                eventText+=",";
            }else if(i==0){
                eventText+="|";
            }
        }
        preparedStatement.close();
        connection.close();
        return eventText;
    }
    
    public void priceChange(int userId) throws Exception{
		ConnectionFactory connectionFactory=new ConnectionFactory();
		Connection connection=connectionFactory.getConnection();
		String sql="select productPrice,housePrice from jc_userproperty where userId=?";
		PreparedStatement preparedStatement;
		preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
		preparedStatement.setInt(1, userId);
		ResultSet resultSet=preparedStatement.executeQuery();
		String product="";
		String house="";
        while(resultSet.next()){
            product=resultSet.getString("productPrice");
            house=resultSet.getString("housePrice");
        }
        ArrayList<String> proList=new ArrayList<String>();
        ArrayList<String> houList=new ArrayList<String>();
        Split split=new Split();
        proList=split.splitList(product);
        houList=split.splitList(house);
        int random=(int)(1+Math.random()*10);
        int index=0;
        for(int i=0;i<proList.size();i++){
        	index=(int)(0+Math.random()*1);
        	if(index==1){
        		random*=1;
        	}else{
        		random*=-1;
        	}
        	proList.set(i, Integer.parseInt(proList.get(i))+random*10+"");
        }
        random=(int)(1+Math.random()*10);
        for(int i=0;i<houList.size();i++){
        	index=(int)(0+Math.random()*1);
        	if(index==1){
        		random*=1;
        	}else{
        		random*=-1;
        	}
        	houList.set(i, Integer.parseInt(houList.get(i))+random*10+"");
        }
        
        String pro="";
        String hou="";
        for(int i=0;i<proList.size();i++){
        	pro+=proList.get(i);
        	if(i!=proList.size()-1){
        		pro+=",";
        	}
        }
        for(int i=0;i<houList.size();i++){
        	hou+=houList.get(i);
        	if(i!=houList.size()-1){
        		hou+=",";
        	}
        }
        
        sql="update jc_userproperty set productPrice =? ,housePrice=? where userId=?";
        preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
		preparedStatement.setString(1, pro);
		preparedStatement.setString(2, hou);
		preparedStatement.setInt(3, userId);
		int priceok=0;
		priceok=preparedStatement.executeUpdate();
	}
	
	public String getRank() throws Exception{
		ConnectionFactory connectionFactory=new ConnectionFactory();
		Connection connection=connectionFactory.getConnection();
		String sql="select userId,money from jc_userproperty order by money DESC";
		PreparedStatement preparedStatement;
		preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
		ResultSet resultSet=preparedStatement.executeQuery();
        ArrayList<String> list=new ArrayList<String>();
        int index=0;
		while(resultSet.next()){
            list.add(resultSet.getInt("userId")+"");
            list.add(resultSet.getInt("money")+"");
            index++;
            if(index==5){
            	break;
            }
        }
		for(int i=0;i<list.size();i++){
			if(i%2==0){
				sql="select userName from jc_user where userId=?";
				preparedStatement=(PreparedStatement)connection.prepareStatement(sql);
				preparedStatement.setInt(1, Integer.parseInt(list.get(i)));
				resultSet=preparedStatement.executeQuery();
				while(resultSet.next()){
					list.set(i, resultSet.getString("userName"));
				}
			}
		}
		String str="";
		for(int i=0;i<list.size();i++){
			str+=list.get(i);
			if(i!=list.size()-1){
				str+=",";
			}
		}
		System.out.println("----------------"+str);
		return str;
	}

}
